/**
 * Created by xie on 2018/4/4.
 */
angular.module('module.auth')
    .controller('ChangepwdController', ChangepwdController);

ChangepwdController.$inject = ['$scope','$state','$cookies','$rootScope','AutherService'];

// 登录
function ChangepwdController($scope, $state,$cookies, $rootScope,AutherService) {

    var vm = this;
    if(!$cookies.get("userInfo")){
        $state.go("login");
    }
    vm.user=JSON.parse($cookies.get("userInfo"));
    vm.updatePwd=updatePwd;
    vm.user.password="";


    function updatePwd() {
        if(vm.user.password.length<6)
        {
            vm.userinfo = null;
            return alert("密码需大于6位");
        }
        if (!vm.user.password&&!vm.user.pwd) {
            vm.userinfo = null;
            return alert("密码不能为空");

        }
        if (vm.user.password != vm.user.pwd) {
            vm.user = null;
            return alert("两次输入的密码不一致");
        }


        console.log("user",vm.user);
        AutherService.updateUser(vm.user).$promise.then(function (data) {
            $cookies.put("userInfo", JSON.stringify(vm.user), {'expires': new Date(new Date().getTime() + 24 * 3600 * 1000)});

            request = data;
            console.log(request)
            alert("修改密码成功");
            $cookies.remove("userInfo");
            $state.go("login");
        }, function (err) {
            console.log(err);
            return alert(err.data.msg);
        });
    }

    }



